<?php

namespace Admin\Controller;

use Think\Controller;

class SortController extends BaseController {

    private $sort;

    public function __construct() {

        parent::__construct();

        //实例化数据库
        $this->sort = D('Sort');
    }

    //添加分类
    public function add() {
        //实例化数据库
        // $class = M('hhj_class');
        $data = I('post.');
        if (IS_GET) {
            $condition = $this->sort->where('status=0')->select();

            $data = $this->sort->findSubTree($condition, 0, 1);
            $data = $this->sort->tree($data);

            $this->assign('data', $data);
            $this->display();
        }
        if (IS_POST) {


            if ($this->sort->create() && $this->sort->add()) {
                $this->success('成功');
            } else {
                $this->error($this->sort->getError());
            }
//                $this->sort->data($data)->add();
//                $this->success('成功');
            // var_dump($id);
        }
    }

    public function lists() {
//        $count = $this->sort->count();
//        $Page = new \Think\Page($count, 100);
//        $Page->setConfig('header', '<span class="rows">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</span>');
//        $Page->setConfig('prev', '上一页');
//        $Page->setConfig('next', '下一页');
//        $Page->setConfig('last', '末页');
//        $Page->setConfig('first', '首页');
//        $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
//        $data = $this->sort->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $data = $this->sort->where('status=0')->select();
        $data = $this->sort->findSubTree($data);
        $data = $this->sort->tree($data);
//        $show = $Page->show();
        $this->assign('data', $data);
//        $this->assign('show', $show);
        $this->display();
    }

    /**
     * 修改分类
     * @return boolean
     */
    public function edit() {
        $id = I('get.id');
        $sort = D('sort');

        if (IS_GET) {

            if (empty($id)) {
                return false;
            } else {
                $where['id'] =$id;
                $where['status']= 0;
                $result = $sort->where('status=0')->select();
                $class = $sort->field('class,id,description')->where($where)->select();
               // var_dump($class);die;
                $result = $sort->findSubTree($result);
                $data = $sort->tree($result);
                $this->assign('data', $data);
                $this->assign('class', $class[0]);

                $this->display();
            }
        } else {
            $data = I('post.');
            if (empty($data['id'])) {
                return false;
            }
            if ($sort->where('id=' . $data['id'])->save($data)) {
                $this->success('成功');
            } else {
                $this->success('失败');
            }
        }
    }

    /**
     * 删除分类
     */
    public function destroy() {
        $id = intval(I('get.id'));
//        var_dump($id);die;
        $sort = D('sort');
        $where['pid'] = $id;
        $where['status'] = 0;
        $res = $sort->where($where)->field('delete_time')->select();
//        var_dump($sort->getLastSql());
//        var_dump(empty($res));die;
        if (!empty($res)) {
            $this->ajaxReturn(1);
//            $this->success('还有子集');
        } else {
            // $data['id'] = $id;
            $data['delete_time'] = date('Y-m-d H:i:s', time());
            $data['status'] = 1;
//            var_dump($data);
            $sort->where('id=' . $id)->data($data)->save();
//            var_dump($sort->getLastSql());
//            die;
//            $this->success('删除成功');
           $this->ajaxReturn(2);
        }
    }

}
